package com.nf.health.app.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nf.health.app.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: OldCallAdapter
 * @Description: sos适配器 * @author Ian
 * @date 2015-11-24 上午9:55:26
 * 
 */
public class OldSosAdapter extends BaseAdapter {
	private List<Map<String, String>> mData = new ArrayList<Map<String, String>>();// 存储的EditText值

	private int currentPos = -1;
	private Context context;
	private List<Map<String, String>> doctors;

	ViewHolder holder;

	public OldSosAdapter(Context context, List<Map<String, String>> doctors) {
		super();
		this.context = context;
		this.doctors = doctors;
		for(int i=0;i<doctors.size();i++){
			mData.add(new HashMap<String, String>());
		}
	}

	@Override
	public int getCount() {
		return doctors.size();
	}

	@Override
	public Object getItem(int position) {
		return doctors.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		holder = new ViewHolder();
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.oldsos_item, null);
			holder.name = (TextView) convertView
					.findViewById(R.id.oldcall_name);
			holder.num = (TextView) convertView.findViewById(R.id.oldcall_no);
			holder.phonenum = (TextView) convertView
					.findViewById(R.id.oldcall_phonenum);
			holder.old_ed_phonenum = (EditText) convertView
					.findViewById(R.id.old_ed_phonenum);
			holder.old_ed_name = (EditText) convertView
					.findViewById(R.id.old_ed_name);
			holder.phonenum = (TextView) convertView
					.findViewById(R.id.oldcall_phonenum);
			holder.next_btn = (Button) convertView.findViewById(R.id.next_btn);
			holder.jia = (ImageView) convertView
					.findViewById(R.id.add_sos_button);
			holder.jian = (ImageView) convertView
					.findViewById(R.id.jian_sos_button);
			holder.oldcall_lin = (LinearLayout) convertView
					.findViewById(R.id.oldcall_lin);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.old_ed_name.setTag(position);
		holder.old_ed_phonenum.setTag(position);

		holder.old_ed_name.addTextChangedListener(new MyTextWatcher(holder));
		holder.old_ed_phonenum.addTextChangedListener(new MyTextWatcher2(holder));


		holder.jia.setTag(position);
		holder.jia.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				if(doctors.size()==5){
					Toast.makeText(context, "手机号已达上限",1000).show();
				}else{
					int pos = (Integer) arg0.getTag();
					currentPos = pos;
					notifyDataSetChanged();
				}
			}
		});

		holder.jian.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				currentPos = -1;
				notifyDataSetChanged();
				if (listener != null)
					listener.onClick(position, "");
			}
		});

		if (currentPos == position) {
			if (holder.oldcall_lin.getVisibility() == View.VISIBLE) {
				holder.oldcall_lin.setVisibility(View.GONE);
			} else {
				holder.oldcall_lin.setVisibility(View.VISIBLE);
			}
		} else {
			holder.oldcall_lin.setVisibility(View.GONE);
		}

		// 保存按钮
		holder.next_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				holder.old_ed_name.clearFocus();
				holder.old_ed_phonenum.clearFocus();
				String name = mData.get(position).get("list_item_name");
				String phone = mData.get(position).get("list_item_phone");
				if (listener2 != null)
					listener2.onClick(position, name, phone, "");
				
			}
		});
		holder.name.setText(doctors.get(position).get("\"name\"")
				.replace("\"", ""));
		holder.num.setText(position+1+"");
		holder.phonenum.setText(doctors.get(position).get("\"num\"")
				.replace("\"", ""));

		return convertView;
	}

	class MyTextWatcher implements TextWatcher {
		public MyTextWatcher(ViewHolder holder) {
			mHolder = holder;
		}

		private ViewHolder mHolder;

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
			if (s != null && !"".equals(s.toString())) {
				int position = (Integer) mHolder.old_ed_name.getTag();
				mData.get(position).put("list_item_name", s.toString());// 当EditText数据发生改变的时候存到data变量中
			}
		}

	}
	class MyTextWatcher2 implements TextWatcher {
		public MyTextWatcher2(ViewHolder holder) {
			mHolder = holder;
		}

		private ViewHolder mHolder;

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
			if (s != null && !"".equals(s.toString())) {
				int position = (Integer) mHolder.old_ed_phonenum.getTag();
				mData.get(position).put("list_item_phone",s.toString());// 当EditText数据发生改变的时候存到data变量中
			}
		}

	}


	static class ViewHolder {
		TextView name, num, phonenum;

		ImageView jia, jian;

		LinearLayout oldcall_lin;

		Button next_btn;

		EditText old_ed_phonenum, old_ed_name;
	}

	private OnMyClickListener listener;

	public void setOnMyClickListener(OnMyClickListener listener) {
		this.listener = listener;
	}

	private OnMyClickListener1 listener2;

	public void setOnMyClickListener1(OnMyClickListener1 listener2) {
		this.listener2 = listener2;
	}

}
